package com.ccc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ccc.dao.RoleProductDao;
import com.ccc.model.Product;
import com.ccc.model.Role;
import com.ccc.model.RoleProduct;
import com.ccc.util.dao.UtilDao;

@Component("roleProductDao")
public class RoleProductDaoImpl implements RoleProductDao {
	private UtilDao utilDao;

	@Override
	public void addRoleProduct(RoleProduct roleProduct) {
		utilDao.save(roleProduct);
	}

	@Override
	public void deleteRoleProduct(RoleProduct roleProduct) {
		utilDao.delete(roleProduct);
	}

	@Override
	public void updateRoleProduct(RoleProduct roleProduct) {
		utilDao.update(roleProduct);
	}

	@Override
	public RoleProduct findRoleProduct(int id) {
		return utilDao.find(RoleProduct.class, id);
	}

	public UtilDao getUtilDao() {
		return utilDao;
	}

	@Resource
	public void setUtilDao(UtilDao utilDao) {
		this.utilDao = utilDao;
	}

	@Override
	public RoleProduct findByRoleAndProduct(Role role, Product product) {
		List<RoleProduct> list = utilDao.find("from RoleProduct rp where rp.role = ? and rp.product = ?", new Object[]{role, product});
		if(list!=null&&list.size()>0){
			return list.get(0);
		}else
			return null;
	}

}
